package controllers.customer;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.OrderService;
import services.PurchasedItemService;
import services.ShoppingCartItemUnitService;
import controllers.AbstractController;
import domain.Order;
import domain.PurchasedItem;
import domain.ShoppingCartItemUnit;

@Controller
@RequestMapping("/order/customer")
public class OrderCustomerController extends AbstractController {

	// Services
	@Autowired
	private OrderService orderService;

	@Autowired
	private PurchasedItemService purchasedItemService;
	@Autowired
	private ShoppingCartItemUnitService shoppingCartItemUnitService;

	// Constructors
	public OrderCustomerController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Order> orders;

		orders = orderService.findBoughtOrders();

		result = new ModelAndView("order/list");
		result.addObject("requestURI", "order/customer/list.do");
		result.addObject("orders", orders);
		result.addObject("backURI", "/welcome/index.do");

		return result;
	}

	// View
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public ModelAndView view(@RequestParam int orderId) {
		ModelAndView result;
		Order order;
		Collection<PurchasedItem> purchasedItems;

		order = orderService.findOne(orderId);
		purchasedItems = purchasedItemService.findByOrder(order);

		result = new ModelAndView("order/view");
		result.addObject("requestURI", "order/customer/view.do");
		result.addObject("order", order);
		result.addObject("purchasedItems", purchasedItems);
		result.addObject("backURI", "/order/customer/list.do");

		return result;
	}

	// Create Order
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create() {
		ModelAndView result;
		Order order;

		Collection<PurchasedItem> purchasedItems;

		try {

			order = orderService.addItemsToOrder();
			purchasedItems = purchasedItemService.findByOrder(order);
			result = new ModelAndView("order/view");
			result.addObject("requestURI", "order/customer/view.do");
			result.addObject("order", order);
			result.addObject("purchasedItems", purchasedItems);
			result.addObject("backURI", "/order/customer/list.do");
		} catch (Throwable oops) {
			result = createEditModelAndView("order.commit.error");
			Collection<ShoppingCartItemUnit> cart = shoppingCartItemUnitService
					.findByCustomer();

			String itemsErrores = "";
			for (ShoppingCartItemUnit s : cart) {

				if (s.getAvailableItem().getAvailableUnits() < s
						.getNumberOfItems()) {
					itemsErrores += " " + s.getAvailableItem().getName();
				}

			}
			result.addObject("itemsErrores1", "order.itemsWithErrors");
			result.addObject("itemsErrores2", itemsErrores);

		}
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView() {
		ModelAndView result;

		result = createEditModelAndView(null);

		return result;
	}

	protected ModelAndView createEditModelAndView(String message) {
		ModelAndView result;
		Collection<ShoppingCartItemUnit> scius;
		Boolean itemsInCart;

		scius = shoppingCartItemUnitService.findByCustomer();
		itemsInCart = !scius.isEmpty();

		result = new ModelAndView("cart/list");
		result.addObject("requestURI", "availableItem/customer/listCart.do");
		result.addObject("scius", scius);
		result.addObject("itemsInCart", itemsInCart);
		result.addObject("backURI", "/welcome/index.do");
		result.addObject("message", message);

		return result;
	}
}
